package com.google.android.play.core.assetpacks;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.ParcelFileDescriptor;
import c.b.a.c.a.c.a;
import c.b.a.c.a.c.d0;
import c.b.a.c.a.c.m;
import c.b.a.c.a.c.z;
import c.b.a.c.a.g.e;
import c.b.a.c.a.g.g;
import c.b.a.c.a.g.p;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

final class u1 implements i3 {
    private static final a g = new a("FakeAssetPackService");

    /* renamed from: a  reason: collision with root package name */
    private final String f2639a;

    /* renamed from: b  reason: collision with root package name */
    private final v f2640b;

    /* renamed from: c  reason: collision with root package name */
    private final Context f2641c;

    /* renamed from: d  reason: collision with root package name */
    private final e2 f2642d;
    private final z<Executor> e;
    private final Handler f = new Handler(Looper.getMainLooper());

    static {
        new AtomicInteger(1);
    }

    u1(File file, v vVar, y0 y0Var, Context context, e2 e2Var, z<Executor> zVar) {
        this.f2639a = file.getAbsolutePath();
        this.f2640b = vVar;
        this.f2641c = context;
        this.f2642d = e2Var;
        this.e = zVar;
    }

    static long d(int i, long j) {
        if (i == 2) {
            return j / 2;
        }
        if (i == 3 || i == 4) {
            return j;
        }
        return 0;
    }

    private static String e(File file) {
        try {
            return w1.b(Arrays.asList(new File[]{file}));
        } catch (NoSuchAlgorithmException e2) {
            throw new com.google.android.play.core.common.a("SHA256 algorithm not supported.", e2);
        } catch (IOException e3) {
            throw new com.google.android.play.core.common.a(String.format("Could not digest file: %s.", new Object[]{file}), e3);
        }
    }

    private final void f(int i, String str, int i2) {
        Bundle bundle = new Bundle();
        bundle.putInt("app_version_code", this.f2642d.a());
        bundle.putInt("session_id", i);
        File[] i3 = i(str);
        ArrayList arrayList = new ArrayList();
        long j = 0;
        for (File file : i3) {
            j += file.length();
            ArrayList arrayList2 = new ArrayList();
            arrayList2.add(i2 == 3 ? new Intent().setData(Uri.EMPTY) : null);
            String a2 = m.a(file);
            bundle.putParcelableArrayList(d0.b("chunk_intents", str, a2), arrayList2);
            bundle.putString(d0.b("uncompressed_hash_sha256", str, a2), e(file));
            bundle.putLong(d0.b("uncompressed_size", str, a2), file.length());
            arrayList.add(a2);
        }
        bundle.putStringArrayList(d0.a("slice_ids", str), arrayList);
        bundle.putLong(d0.a("pack_version", str), (long) this.f2642d.a());
        bundle.putInt(d0.a("status", str), i2);
        bundle.putInt(d0.a("error_code", str), 0);
        bundle.putLong(d0.a("bytes_downloaded", str), d(i2, j));
        bundle.putLong(d0.a("total_bytes_to_download", str), j);
        bundle.putStringArrayList("pack_names", new ArrayList(Arrays.asList(new String[]{str})));
        bundle.putLong("bytes_downloaded", d(i2, j));
        bundle.putLong("total_bytes_to_download", j);
        this.f.post(new t1(this, new Intent("com.google.android.play.core.assetpacks.receiver.ACTION_SESSION_UPDATE").putExtra("com.google.android.play.core.assetpacks.receiver.EXTRA_SESSION_STATE", bundle)));
    }

    private final File[] i(String str) {
        File file = new File(this.f2639a);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles(new s1(str));
            if (listFiles != null) {
                if (r1 != 0) {
                    for (File a2 : listFiles) {
                        if (m.a(a2).equals(str)) {
                            return listFiles;
                        }
                    }
                    throw new com.google.android.play.core.common.a(String.format("No master slice available for pack '%s'.", new Object[]{str}));
                }
                throw new com.google.android.play.core.common.a(String.format("No APKs available for pack '%s'.", new Object[]{str}));
            }
            throw new com.google.android.play.core.common.a(String.format("Failed fetching APKs for pack '%s'.", new Object[]{str}));
        }
        throw new com.google.android.play.core.common.a(String.format("Local testing directory '%s' not found.", new Object[]{file}));
    }

    public final e<List<String>> a() {
        g.f("syncPacks()", new Object[0]);
        return g.b(new ArrayList());
    }

    public final void b(int i, String str) {
        g.f("notifyModuleCompleted", new Object[0]);
        this.e.a().execute(new r1(this, i, str));
    }

    public final void c() {
        g.f("keepAlive", new Object[0]);
    }

    /* access modifiers changed from: package-private */
    public final /* synthetic */ void g(Intent intent) {
        this.f2640b.a(this.f2641c, intent);
    }

    /* access modifiers changed from: package-private */
    public final /* synthetic */ void h(int i, String str) {
        try {
            f(i, str, 4);
        } catch (com.google.android.play.core.common.a e2) {
            g.g("notifyModuleCompleted failed", e2);
        }
    }

    public final void j(List<String> list) {
        g.f("cancelDownload(%s)", list);
    }

    public final void o(int i) {
        g.f("notifySessionFailed", new Object[0]);
    }

    public final e<ParcelFileDescriptor> p(int i, String str, String str2, int i2) {
        g.f("getChunkFileDescriptor(session=%d, %s, %s, %d)", Integer.valueOf(i), str, str2, Integer.valueOf(i2));
        p pVar = new p();
        try {
            for (File file : i(str)) {
                if (m.a(file).equals(str2)) {
                    pVar.c(ParcelFileDescriptor.open(file, 268435456));
                    return pVar.a();
                }
            }
            throw new com.google.android.play.core.common.a(String.format("Local testing slice for '%s' not found.", new Object[]{str2}));
        } catch (FileNotFoundException e2) {
            g.g("getChunkFileDescriptor failed", e2);
            pVar.b(new com.google.android.play.core.common.a("Asset Slice file not found.", e2));
        } catch (com.google.android.play.core.common.a e3) {
            g.g("getChunkFileDescriptor failed", e3);
            pVar.b(e3);
        }
    }

    public final void q(int i, String str, String str2, int i2) {
        g.f("notifyChunkTransferred", new Object[0]);
    }
}
